import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class SnakeMain extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2871489214L;
	private JMenuBar mb;
	private Snake snake;
	private GamePanel gamepanel;
	private ScorePanel scorepanel;
	private JButton but, but1;

	public SnakeMain() {
		setTitle("贪吃蛇游戏");
		setSize(700, 568);
		Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension framesize = this.getSize();
		int x = (int) screensize.getWidth() / 2 - (int) framesize.getWidth() / 2;
		int y = (int) screensize.getHeight() / 2 - (int) framesize.getHeight() / 2;
		setLocation(x, y);
		setLayout(null);
		setVisible(true);


		Font font = new Font("宋体", Font.BOLD, 15);
		Font font1 = new Font("宋体", Font.TRUETYPE_FONT, 13);

		// 创建菜单栏
		String[] menu1 = { "游戏", "速度", "帮助" };
		String[][] item = { { "新游戏"}, { "高速", "中速", "低速" }, { "关于" } };
		mb = new JMenuBar();
		for (int i = 0; i < menu1.length; i++) {
			JMenu menu = new JMenu(menu1[i]);
			menu.setFont(font);
			mb.add(menu);
			for (int j = 0; j < item[i].length; j++) {
				JMenuItem menuitem = new JMenuItem(item[i][j]);
				menuitem.setFont(font1);
				menuitem.addActionListener(this);
				menu.add(menuitem);
			}
		}
		setJMenuBar(mb);

		// 创建按钮
		but = new JButton("开始");
		but.setBounds(525, 315, 130, 50);
		but.setFont(font);
		but.addActionListener(this);
		add(but);

		but1 = new JButton("暂停");
		but1.setBounds(525, 325 + 85, 130, 50);
		but1.setFont(font);
		but1.addActionListener(this);
		add(but1);

		snake = new Snake();
		// 创建分数面板
		scorepanel = new ScorePanel(snake);
		scorepanel.setLocation(520, 0);
		add(scorepanel);

		/**
		 * 创建游戏面板。 游戏面板对键盘进行监听。
		 */
		gamepanel = new GamePanel(snake);
		gamepanel.setLocation(0, 0);
		add(gamepanel);

		gamepanel.setFocusable(true); // 设置游戏面板监听聚焦
		gamepanel.requestFocus();
		gamepanel.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				super.keyPressed(e);
				int code = e.getKeyCode();
				switch (code) {
				case KeyEvent.VK_UP:
					snake.change(Snake.UP);
					break;
				case KeyEvent.VK_DOWN:
					snake.change(Snake.DOWN);
					break;
				case KeyEvent.VK_LEFT:
					snake.change(Snake.LETF);
					break;
				case KeyEvent.VK_RIGHT:
					snake.change(Snake.RIGHT);
					break;
				default:
					break;
				}
			}
		});
        
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				new SnakeMain();
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String Com = e.getActionCommand();
		if (Com == "关于") {
			JOptionPane.showMessageDialog(null, "Author： chenjia" + "\n" + "Version: 1.0" + "\n" + "email: 1234567@qq.com");
		}

		// 游戏按钮事件
		if (Com == "新游戏") {
			snake.initSnake();
			scorepanel.initTxt();
			but.setEnabled(true);
			gamepanel.repaint();
		}

		// 开始按钮事件
		if (Com == "开始") {
			// 并行启动线程
			snake.setIsquit(false);
			gamepanel.gameStart();
			scorepanel.countStart();
			snake.setTxtstate("运行中...");
			//设置游戏面板聚焦
			gamepanel.setFocusable(true);
			gamepanel.requestFocus();
		
			
			but1.setEnabled(true);
			but.setEnabled(false);
			but.setFocusable(false); // 设置监听为false.
		}

		// 暂停按钮事件
		if (Com == "暂停") {
			if(snake.getIsbuttonactive()) {but1.setEnabled(false);return;}
			snake.setIsquit(true);
			snake.setTxtstate("暂停");
			but.setEnabled(true);
			but1.setEnabled(false);
			but1.setFocusable(false);// 设置监听为false.
		}

		// 速度选择事件
		switch (Com) {
		case "高速":
			snake.setSpend(100);
			break;
		case "中速":
			snake.setSpend(200);
			break;
		case "低速":
			snake.setSpend(500);
			break;
		case "音乐":
		
			break;
		default:
			break;
		}
	}
}
